﻿using System;
using System.Data;
using System.IO;
using System.Web.UI;
using BLL;
using DevExpress.Web;
using Entity;
using NPOI.HSSF.UserModel;
using Web.Class;

namespace Web.Pages.YongShui._15.BaoBiaoGL.DanJuDY
{
    public partial class JiHuaZXQK : Page
    {
        /// <summary>
        ///     管理块BLL
        /// </summary>
        private readonly YS_JX_GUANLIKBLL _YS_JX_GUANLIKBLL = new YS_JX_GUANLIKBLL();

        /// <summary>
        ///     保存结果集1
        /// </summary>
        private DataTable dtGrid
        {
            get { return ViewState["dtGrid"] as DataTable; }
            set { ViewState["dtGrid"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                InitData();
            ScriptManager1.RegisterPostBackControl(btnExport);
            if (dtGrid != null)
            {
                gvYongHuXX.DataSource = dtGrid;
                gvYongHuXX.DataBind();
            }
        }

        protected void btnExport_OnClick(object sender, EventArgs e)
        {
            if (dtGrid == null || dtGrid.Rows.Count < 1)
                PageTools.AlertMsg(this, "没有符合的数据导出");
            else
                ExportExcel(dtGrid);
        }

        protected void btnDingWei_OnClick(object sender, EventArgs e)
        {
            if (txtYongHuH.Text.Trim() != "")
            {
                var id = PageTools.FocusOpreateRecord(dtGrid, gvYongHuXX, "S_YONGHUH", txtYongHuH.Text, true);
                if (id == -1)
                    PageTools.AlertMsg(this, "当前数据中没有你要找的数据,请检查你输入的条件是否正确。");
            }
        }

        /// <summary>
        ///     初始化数据
        /// </summary>
        private void InitData()
        {
            gvYongHuXX.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            //水量年月
            var water_y_m = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
            txtShuiLiangYM.Text = water_y_m;
            var dtGuanLiK = _YS_JX_GUANLIKBLL.SelectYS_JX_GUANLIKsDynamic("i_jlzt=0", "S_GUANLIKH");
            cmbGuanLiK.Items.Add("全部", "0");
            for (var i = 0; i < dtGuanLiK.Rows.Count; i++)
                cmbGuanLiK.Items.Add(dtGuanLiK.Rows[i]["S_GUANLIKMC"].ToString(),
                    dtGuanLiK.Rows[i]["S_GUANLIKH"].ToString());

            cmbGuanLiK.SelectedIndex = 0;
        }

        private void LoadDataGrid()
        {
            var guanlikuai = cmbGuanLiK.SelectedItem.Value.ToString();
            var year = txtShuiLiangYM.Text.Substring(0, 4);
            var month = txtShuiLiangYM.Text.Substring(4, 2);
            var tuoshou = cmbShouFeiFS.SelectedItem.Value.ToString();
            var tiaozheng = cmbTiaoZheng.SelectedItem.Value.ToString();
            dtGrid = new SqlHelper().GetJiHuaZXQK(guanlikuai, year, month, tuoshou, tiaozheng).Tables[0];
            gvYongHuXX.DataSource = dtGrid;
            gvYongHuXX.DataBind();
            if (dtGrid != null && dtGrid.Rows.Count > 0)
            {
                zjl.Text = "共" + dtGrid.Rows.Count + "条";
                gvYongHuXX.FocusedRowIndex = 0;
            }
        }

        protected void btnChaXun_OnClick(object sender, EventArgs e)
        {
            LoadDataGrid();
        }

        private void ExportExcel(DataTable dt)
        {
            var wb = new HSSFWorkbook();
            var sheet = (HSSFSheet) wb.CreateSheet("Sheet1"); //创建工作表
            sheet.CreateFreezePane(0, 1); //冻结列头行
            var rowTitle = (HSSFRow) sheet.CreateRow(0); //创建列头行
            var columns = new[]
            {
                "用户编号", "用水量", "加价水量", "加价金额", "用户名"
            };
            for (var i = 0; i < columns.Length; i++)
            {
                var cellTitle = (HSSFCell) rowTitle.CreateCell(i);
                cellTitle.SetCellValue(columns[i]);
            }

            var index = 1;
            foreach (DataRow row in dt.Rows)
            {
                var rowData = (HSSFRow) sheet.CreateRow(index);
                for (var i = 0; i < row.ItemArray.Length; i++)
                {
                    var cellData = (HSSFCell) rowData.CreateCell(i);
                    cellData.SetCellValue(row[i].ToString());
                }

                index++;
            }

            var stream = new MemoryStream();
            wb.Write(stream);
            var buf = stream.ToArray();
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + txtShuiLiangYM.Text + "计划执行情况.xls");
            Response.BinaryWrite(buf);
        }
    }
}